package com.yanqu.road.server.manager.palace;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.estatewar.palace.PalaceAideJoinApply;
import com.yanqu.road.entity.estatewar.palace.PalaceUserData;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossPalaceActivityMgr extends TempMgr {
    private static Map<Integer, CrossPalaceActivity> crossPalaceActivityMap = new ConcurrentHashMap<>();
    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        crossPalaceActivityMap = new ConcurrentHashMap<>();
        synchronized (crossPalaceActivityMap) {
            Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossActivityMapByType(eActivityType.PalaceActivity.getValue());
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                CrossPalaceActivity crossPalaceActivity = new CrossPalaceActivity();
                if (!crossPalaceActivity.init(activityInfo)) {
                    getLogger().error("load palace activity fail");
                } else {
                    crossPalaceActivityMap.put(activityInfo.getActivityId(), crossPalaceActivity);
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for(CrossPalaceActivity crossPalaceActivity : crossPalaceActivityMap.values()){
            crossPalaceActivity.save();
        }
        return true;
    }

    public static boolean reloadActivity(){
        synchronized (crossPalaceActivityMap) {
            Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossActivityMapByType(eActivityType.PalaceActivity.getValue());
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                CrossPalaceActivity crossPalaceActivity = crossPalaceActivityMap.get(activityInfo.getActivityId());
                if (null == crossPalaceActivity) {
                    crossPalaceActivity = new CrossPalaceActivity();
                    if (!crossPalaceActivity.init(activityInfo)) {
                        getLogger().error("load palace activity fail");
                    } else {
                        crossPalaceActivityMap.put(activityInfo.getActivityId(), crossPalaceActivity);
                    }
                } else {
                    crossPalaceActivity.reloadActivity(activityInfo);
                }
            }

            //清除过期活动
            List<Integer> removeList = new ArrayList<>();
            for(CrossPalaceActivity crossPalaceActivity : crossPalaceActivityMap.values()){
                if(!crossPalaceActivity.isPalaceActivityInTime()){
                    removeList.add(crossPalaceActivity.getActivityId());
                }
            }
            if(removeList.size() > 0){
                synchronized (crossPalaceActivityMap){
                    for(int aid : removeList) {
                        crossPalaceActivityMap.remove(aid);
                    }
                }
            }
        }
        return true;
    }

    public static CrossPalaceActivity getCrossPalaceActivity(int activityId){
        if(!crossPalaceActivityMap.containsKey(activityId)){
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if(null == activityInfo){
                getLogger().error("getCrossPalaceActivity fail, activityId:{}", activityId);
                return null;
            }
            reloadActivity();
        }
        return crossPalaceActivityMap.get(activityId);
    }

    public static EstateWarProto.PalaceAideListMsg.Builder parsePalaceAideListMsg(List<PalaceUserData> dataList){
        EstateWarProto.PalaceAideListMsg.Builder listMsg = EstateWarProto.PalaceAideListMsg.newBuilder();
        if(null != dataList){
            for(PalaceUserData data : dataList){
                EstateWarProto.PalaceAideTempMsg.Builder msg = parsePalaceAideTempMsg(data);
                if(null != msg){
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }

    public static EstateWarProto.PalaceAideTempMsg.Builder parsePalaceAideTempMsg(PalaceUserData data){
        if(null == data){
            return null;
        }
        EstateWarProto.PalaceAideTempMsg.Builder msg = EstateWarProto.PalaceAideTempMsg.newBuilder();
        msg.setUserId(data.getUserId());
        msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(CrossUserMgr.getUserBaseInfo(data.getUserId())));
        return msg;
    }

    public static EstateWarProto.PalaceAideApplyListMsg.Builder parsePalaceAideApplyListMsg(List<PalaceAideJoinApply> dataList){
        EstateWarProto.PalaceAideApplyListMsg.Builder listMsg = EstateWarProto.PalaceAideApplyListMsg.newBuilder();
        if(null != dataList){
            for(PalaceAideJoinApply data : dataList){
                EstateWarProto.PalaceAideApplyTempMsg.Builder msg = parsePalaceAideApplyTempMsg(data);
                if(null != msg){
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }

    public static EstateWarProto.PalaceAideApplyTempMsg.Builder parsePalaceAideApplyTempMsg(PalaceAideJoinApply data){
        if(null == data){
            return null;
        }
        EstateWarProto.PalaceAideApplyTempMsg.Builder msg = EstateWarProto.PalaceAideApplyTempMsg.newBuilder();
        msg.setApplyUid(data.getApplyUid());
        msg.setApplyTime(data.getApplyTime());
        msg.setUserId(data.getApplyUserId());
        msg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(CrossUserMgr.getUserBaseInfo(data.getApplyUserId())));
        return msg;
    }
}
